var ctx=window.daijia.ctx;
var detailedJobType=window.detailedJobType;
$(function () {
	$("#licenseTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true ,endDate:new Date()});
	 //公司修改后对应的车辆类型也修改
    $("#companyId").change(function(){
    	var company_id=$("#companyId").val();
    	$("#companyName").val($("#companyId").find("option:selected").text());
    	$("#detailedJobType_div").html("");
    	$.get(ctx+'/driverapply/queryOrderType',{'companyId':company_id},function(result){
    		if(result.success){
    			$(result.data).each(function(index,element){
					var id=result.data[index].id;
					var typeName=result.data[index].typeName;
					var checked="";
					if(detailedJobType.length!=0){
						if (detailedJobType.indexOf(id)!=-1) {
							checked="checked='checked'";
						}
					}
					
	            	var text="<label><input type='checkbox' name='JobType' id='"+id+"' value='"+id+"' "+checked+"/>"+typeName+" </label>";
	            	$("#detailedJobType_div").append(text);
				 });
    		}else{
    		    $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
    		}
    	
    	},'json');
    });
    
    $("input[name=driverJobType]").change(function(){
    	if($(this).val()=="paotui"){
    		if($(this).is(':checked')){
        		$("#detailedJobType_show_div").show();
        	}
    		else{
    			$("#detailedJobType_show_div").hide();
    		}
    	}
    });
    
	//提交表单
    $("#submit_form").submit(function(){
    	var spCodesTemp = "";
    	$('input:checkbox[name=JobType]:checked').each(function(i){
	       if(0==i){
	        spCodesTemp = $(this).val();
	       }else{
	        spCodesTemp += (","+$(this).val());
	       }
    	});
    	$("#detailedJobType").val(spCodesTemp);
    	
    	var username=$("#username").val();
    	var name=$("#name").val();
    	var realname=$("#realname").val();
    	
    	var feifaPattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
		
    	if($.trim(username)==""){
    		$.scojs_message("请填写正确的工号", $.scojs_message.TYPE_ERROR);
			return false;
    	}
    	if(feifaPattern.test(username)){
			$.scojs_message("工号不能含有非法字符", $.scojs_message.TYPE_ERROR);
			return false;
		}
    	
    	if($.trim(name)==""){
    		$.scojs_message("请填写正确的昵称", $.scojs_message.TYPE_ERROR);
			return false;
    	}
		if(feifaPattern.test(name)){
			$.scojs_message("昵称不能含有非法字符", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($.trim(realname)==""){
    		$.scojs_message("请填写正确的真实姓名", $.scojs_message.TYPE_ERROR);
			return false;
    	}
		if(feifaPattern.test(realname)){
			$.scojs_message("真实姓名不能含有非法字符", $.scojs_message.TYPE_ERROR);
			return false;
		}
    	
		var reg=new RegExp("[\\u4E00-\\u9FFF]+","g");
		if(reg.test(realname)){
			//漢字
			if(realname.length>4){
				$.scojs_message("真实姓名长度最多为4个汉字", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		else{
			if(realname.length>8){
				$.scojs_message("真实姓名使用非汉字最多8个字符", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
    	
    	var idCard=$("#idCard").val();
    	var idcardResult=IdCardValidate(idCard);
    	if(!idcardResult){
    		$.scojs_message("身份证号码格式错误", $.scojs_message.TYPE_ERROR);
//    		return false;
    	}
		
    	var bankCard=$("#bankCard").val();
    	if(bankCard == null || bankCard == ""){
    		$.scojs_message("绑定支付账号不能为空", $.scojs_message.TYPE_ERROR);
//    		return false;
    	}
    	
		var phone=$("#phone").val();
		var isphone=/^\+?\d{3,13}(\-?(\d{6,8}?))?(\-?(\d{4,8}?))*$/; 
		if (!isphone.test(phone)){
			$.scojs_message("手机号格式不正确", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#licenseTime").val()==''){
			$.scojs_message("领证时间不能为空", $.scojs_message.TYPE_ERROR);
			return false;
		}
    	
		$("#username").val($.trim($("#username").val()));
		$("#name").val($.trim($("#name").val()));
		$("#realname").val($.trim($("#realname").val()));
		
		$("#submit_button").attr("disabled",true);
		
		$.post($("#submit_form").attr("action"),$("#submit_form").serialize(),function(result){
			if(result.success){
				$.scojs_message("审核成功", $.scojs_message.TYPE_OK);
				window.location=ctx+'/driverapply/list';
			}
			else{
				$.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
				$("#submit_button").attr("disabled",false);
			}
		});
		
		return false;
    }); 
    
});